package com.example.lopngoaingu;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity {

	private TextView tvResult, tvDesc, tvIndex;
	private List<Question> data;
	private int mIndex = 0, mCount = 300;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_layout);
		data = new ArrayList<Question>();
		tvResult = (TextView) findViewById(R.id.tvResult);
		tvDesc = (TextView) findViewById(R.id.tvDesc);
		CreateNumber();
		tvDesc.setText(data.get(mIndex).getDesc());

		tvIndex = (TextView) findViewById(R.id.tvIndex);
		tvIndex.setText(mIndex + 1 + "/" + mCount);
	}

	private void CreateNumber() {
		Character[] operators = new Character[] { '+', '-' };
		for (int i = 0; i < mCount; i++) {
			Random ranA = new Random();
			int numA = 20 + ranA.nextInt((100 - 20) + 1);

			Random op = new Random();
			int opa = op.nextInt(1);

			Random ranB = new Random();
			int numB = ranB.nextInt(100);

			int re = 0;
			if (opa == 0)
				re = numA + numB;
			else if (opa == 1)
				re = numA - numB;
			Question item = new Question(numA + " " + operators[opa] + " "
					+ numB, re);
			data.add(item);
		}
	}

	public void btnClick(View v) {
		switch (v.getId()) {
		case R.id.btnClear:
			tvResult.setText("");
			break;
		default:
			if (mIndex < mCount-1) {
				String num = tvResult.getText().toString();
				num += ((Button) v).getText().toString();
				tvResult.setText(num);
				String s = String.valueOf(data.get(mIndex).getResult());
				if (tvResult.getText().length() == s.length()) {
					if (tvResult.getText().toString().equals(s)) {
						// Toast.makeText(TestActivity.this, "Equals",
						// Toast.LENGTH_SHORT).show();
						mIndex++;
						tvIndex.setText(mIndex + 1 + "/" + mCount);
						tvDesc.setText(data.get(mIndex).getDesc());
						tvResult.setText("");
					}
				}
			}
			break;
		}
	}

	class Question {
		private String desc;
		private int result;

		public Question(String desc, int result) {
			this.desc = desc;
			this.result = result;
		}

		public String getDesc() {
			return desc;
		}

		public void setDesc(String desc) {
			this.desc = desc;
		}

		public int getResult() {
			return result;
		}

		public void setResult(int result) {
			this.result = result;
		}
	}
}
